package unicall.em.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import unicall.em.dtos.SkillDto;
import unicall.em.models.Skill;
import unicall.em.repositories.SkillRepository;
import unicall.em.utils.HttpClientUtil;

@Service
public class SkillServiceImpl implements SkillService {
    @Autowired
    private SkillRepository repository;
    
    @Override
    public Skill add(SkillDto dto) {
        Skill skill  = new Skill();
        skill.setName(dto.getName());
        skill.setQueueId(dto.getQueueId()+"");
        skill.setMaxNum(dto.getMaxNum());
        skill.setMediaId(dto.getMediaId());
        skill.setExitKey(dto.getExitKey());
        skill.setTimeoutLen(dto.getTimeoutLen());
        skill.setTimeoutMediaId(dto.getTimeoutMediaId());
        skill.setContinuteKey(dto.getContinuteKey());
        skill.setMaxQueueCount(dto.getMaxQueueCount()+"");
        skill.setTipMediaId(dto.getTipMediaId());
        skill.setStrategy(dto.getStrategy());
        skill.setNoAnswerTimeout(dto.getNoAnswerTimeout());
        skill.setAcwTimeout(dto.getAcwTimeout());
        skill.setSequenceRing(dto.getSequenceRing());
        return repository.saveAndFlush(skill);
    }

    @Override
    public Skill findById(String id) {
        return repository.findOne(id);
    }

    @Override
    public List<Skill> findAll() {
       return repository.findAll();
    }

    @Override
    public Skill update(SkillDto dto) {
        Skill skill  = findById(dto.getId());
        skill.setName(dto.getName());
        skill.setQueueId(dto.getQueueId()+"");
        skill.setMaxNum(dto.getMaxNum());
        skill.setMediaId(dto.getMediaId());
        skill.setExitKey(dto.getExitKey());
        skill.setTimeoutLen(dto.getTimeoutLen());
        skill.setTimeoutMediaId(dto.getTimeoutMediaId());
        skill.setContinuteKey(dto.getContinuteKey());
        skill.setMaxQueueCount(dto.getMaxQueueCount()+"");
        skill.setTipMediaId(dto.getTipMediaId());
        skill.setStrategy(dto.getStrategy());
        skill.setNoAnswerTimeout(dto.getNoAnswerTimeout());
        skill.setAcwTimeout(dto.getAcwTimeout());
        skill.setSequenceRing(dto.getSequenceRing());
        skill.setState(dto.getState());
        return repository.saveAndFlush(skill);
    }

    @Override
    public void destroy(String id) {
        repository.delete(id);
        
    }

    @Override
    public JSONObject createSkill(SkillDto dto) {
        String url=CREATE_URL;
        HttpClientUtil util = new HttpClientUtil();
        Map<String,String> params = dto.toParams();
        JSONObject obj = null;
        try{
            String result = util.doPost(url, params);
            obj = JSONObject.parseObject(result);
        }catch(Exception e){
        }
        return obj;
    }
    
    @Override
    public JSONObject updateSkill(SkillDto dto) {
        String url=UPDATE_URL;
        HttpClientUtil util = new HttpClientUtil();
        Map<String,String> params = dto.toParams();
        JSONObject obj = null;
        try{
            String result = util.doPost(url, params);
            obj = JSONObject.parseObject(result);
        }catch(Exception e){
           //.setState(Media.State.ERROR.getState()); 
        }
        return obj;
    }
    
    
    public JSONObject destroySkill(SkillDto dto){
        String url = DESTROY_URL;
        HttpClientUtil util = new HttpClientUtil();
        Map<String,String> params = new HashMap<String,String>();
        params.put("id", dto.getId());
        params.put("appId", dto.getAppId());
        params.put("queueId", dto.getQueueId()+"");
        JSONObject obj = null;
        try{
            String result = util.doPost(url, params);
            obj = JSONObject.parseObject(result);
           System.out.println(obj);
        }catch(Exception e){
           //.setState(Media.State.ERROR.getState()); 
        }
      //  repository.saveAndFlush(media);
        return obj;
        
    }

    @Override
    public Page<Skill> findAll(Specification<Skill> specation, Pageable pageable) {
        return repository.findAll(specation, pageable);
    }
    
    
    private final static String CREATE_URL="http://127.0.0.1:9011/skill/create";
    private final static String UPDATE_URL="http://127.0.0.1:9011/skill/update";
    private final static String DESTROY_URL="http://127.0.0.1:9011/skill/destroy";


    
}
